package com.monco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.monco.entity.User;
import com.monco.mapper.UserMapper;
import com.monco.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com/monco
 * @since 2020-04-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> getUserList(User user) {
        return userMapper.selectList(getWrapper(user));
    }

    @Override
    public IPage<User> getUserPage(IPage<User> page, User user) {
        return userMapper.selectPage(page, getWrapper(user));
    }

    private QueryWrapper<User> getWrapper(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(user.getNickName())) {
            queryWrapper.like("nick_name", user.getNickName());
        }
        if (user.getUserType() != null) {
            queryWrapper.eq("user_type", user.getUserType());
        }
        if (StringUtils.isNotBlank(user.getUsername())) {
            queryWrapper.eq("username", user.getUsername());
        }
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }
}
